package cn.lblbc.webbrowser.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import cn.lblbc.webbrowser.R
import cn.lblbc.webbrowser.views.ZoomableImageView
import java.io.File

class ImageViewerAdapter(
    private val imageFiles: List<File>
) : RecyclerView.Adapter<ImageViewerAdapter.ImageViewHolder>() {

    class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ZoomableImageView = itemView.findViewById(R.id.fullscreenImageView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_fullscreen_image, parent, false)
        return ImageViewHolder(view)
    }

    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val imageFile = imageFiles[position]
        
        // 使用Glide加载全屏图片
        Glide.with(holder.itemView.context)
            .load(imageFile)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.ic_image_placeholder)
            .error(R.drawable.ic_image_placeholder)
            .fitCenter()
            .into(holder.imageView)
    }

    override fun getItemCount(): Int = imageFiles.size
}